#include "stdafx.h"
#include "DBFDef.h"
#include "DBFException.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _DEBUG
BOOL TGAssertFailedLine(LPCSTR lpszDBFCall, LPCSTR lpszFile, int nLine)
{
	if (afxTraceFlags & traceDatabase)
	{
		TRACE0("\nDBF Call Failed.");
		TRACE1("\n\t%hs", lpszDBFCall);
		TRACE2("\n\tIn file %hs on line %d", lpszFile, nLine);
	}
	return AfxAssertFailedLine(lpszFile, nLine);
}
#endif // _DEBUG

#ifdef _DEBUG
void CDBFErrorInfo::Dump(CDumpContext& dc) const
{
	dc << "a CDaoErrorInfo at " << (void*)this;

	dc << "\nm_lErrorCode = " << m_lErrorCode;
//	dc << "\nm_strSource = " << m_strSource;
	dc << "\nm_strDescription = " << m_strDescription;
//	dc << "\nm_strHelpFile = " << m_strHelpFile;
//	dc << "\nm_lHelpContext = " << m_lHelpContext;

	dc << "\n";
}
#endif // _DEBUG

IMPLEMENT_DYNAMIC(CDBFException, CException)

CDBFException::CDBFException()
{
	m_pErrorInfo = NULL;
}

CDBFException::~CDBFException()
{
	delete m_pErrorInfo;
	m_pErrorInfo = NULL;
}

// Operations
void CDBFException::GetErrorInfo()
{
	FillErrorInfo();
}

// Implementation
void CDBFException::FillErrorInfo()
{
	// Allocate the error info structure if necessary
	if (m_pErrorInfo == NULL)
		m_pErrorInfo = new CDBFErrorInfo;

	m_pErrorInfo->m_lErrorCode = m_nTGDBFError;
}

BOOL CDBFException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
	PUINT pnHelpContext)
{
	if (pnHelpContext != NULL)
		*pnHelpContext = 0;

	CString strError;
	if(!strError.LoadString(m_nTGDBFError))
	{
		strError.Format(_T("00000000%X"),m_nTGDBFError);
		strError = "Error number 0x" + strError.Right(8) + ".";
	}

	lstrcpyn(lpszError, strError, nMaxError-1);
	lpszError[nMaxError-1] = '\0';
	return TRUE;
}

void AFXAPI TGThrowDBFException(long nTGDBFError)
{
	CDBFException* pException;
	pException = new CDBFException;

	pException->m_nTGDBFError = nTGDBFError;

	THROW(pException);
}
